/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    public partial class PageVendorAccountImp : PageImpBaseClass
    {
        /// <summary>
        /// Needed to populate the login type drop down.
        /// </summary>
        List<String> listLoginType = new List<String>();

        /// <summary>
        /// Surely nobody has more than 30 children for tax purposes?
        /// </summary>
        const int CONST_MAX_DEPENDANTS = 30;

        const int CONST_MIN_BOH_PASSWORD_LEN = 7;

        DbLinkedTextBox tbaddress1TextBox;
        DbLinkedTextBox tbaddress2TextBox;
        DbLinkedTextBox tbstateTextBox;
        DbLinkedTextBox tbcityTextBox;
        DbLinkedTextBox tbfirstNameTextBox;
        DbLinkedTextBox tblastNameTextBox;
        DbLinkedTextBox tbssnTextBox;
        DbLinkedTextBox tbtelephoneTextBox;
        DbLinkedTextBox tbzipcodeTextBox;
        DbLinkedTextBox tbCompanyName;
        ComboBox comboBoxName;

        DbLinkedRadioButton radioNoLimit;
        DbLinkedTextBox tbHighLimit;

        /// <summary>
        /// Constructor
        /// </summary>
        public PageVendorAccountImp(UIElementCollection userInfoGridChild, DbLinkedTextBox tbaddress1TextBox, DbLinkedTextBox tbaddress2TextBox, DbLinkedTextBox tbstateTextBox, DbLinkedTextBox tbcityTextBox, DbLinkedTextBox tbfirstNameTextBox, DbLinkedTextBox tblastNameTextBox, DbLinkedTextBox tbCompanyName, DbLinkedTextBox tbssnTextBox, DbLinkedTextBox tbtelephoneTextBox, DbLinkedTextBox tbzipcodeTextBox, ComboBox comboBoxName, DbLinkedRadioButton radioNoLimit, DbLinkedTextBox tbHighLimit)
        {
            this.ButtonNumber = "27";
            this.ButtonLabelText = "Vendor Accounts";

            this.tbaddress1TextBox = tbaddress1TextBox;
            this.tbaddress2TextBox = tbaddress2TextBox;
            this.tbstateTextBox = tbstateTextBox;
            this.tbcityTextBox = tbcityTextBox;
            this.tbfirstNameTextBox = tbfirstNameTextBox;
            this.tblastNameTextBox = tblastNameTextBox;
            this.tbssnTextBox = tbssnTextBox;
            this.tbtelephoneTextBox = tbtelephoneTextBox;
            this.tbzipcodeTextBox = tbzipcodeTextBox;
            this.comboBoxName = comboBoxName;
            this.tbCompanyName = tbCompanyName;
            this.radioNoLimit = radioNoLimit;
            this.tbHighLimit = tbHighLimit;


            ///Register the controls and lock the form.
            RegisterUiElementsList(userInfoGridChild);

            ///Add all text boxes to the list.
            mTextBoxesList.Add(tbaddress1TextBox);
            mTextBoxesList.Add(tbaddress2TextBox);
            mTextBoxesList.Add(tbstateTextBox);
            mTextBoxesList.Add(tbcityTextBox);
            mTextBoxesList.Add(tbfirstNameTextBox);
            mTextBoxesList.Add(tblastNameTextBox);
            mTextBoxesList.Add(tbCompanyName);
            mTextBoxesList.Add(tbssnTextBox);
            mTextBoxesList.Add(tbtelephoneTextBox);
            mTextBoxesList.Add(tbzipcodeTextBox);

            ///Add the comboboxes.
            ListofComboboxes.Add(comboBoxName);

            ///If the selection changes lock the form down.
            mEntityAddDeleteLoggingEnabled = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            return new List<EntityObject>(Context.VENDOR_ACCOUNTS.ToList());
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void textBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (sender is TextBox)
            {
                (sender as TextBox).SelectAll();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override bool SaveMe()
        {
            if (VerifyFormEntries())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            VENDOR_ACCOUNT selected = comboBoxName.SelectedItem as VENDOR_ACCOUNT;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        public override void MarkEntityDeleted()
        {
            VENDOR_ACCOUNT selected = comboBoxName.SelectedItem as VENDOR_ACCOUNT;
            selected.IsDeleted = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            ///Create the query object.
            var query = from VENDOR_ACCOUNT in Context.VENDOR_ACCOUNTS orderby VENDOR_ACCOUNT.LastName, VENDOR_ACCOUNT.FirstName where VENDOR_ACCOUNT.IsDeleted == false select VENDOR_ACCOUNT;

            ObjectResult<VENDOR_ACCOUNT> result = (ObjectResult<VENDOR_ACCOUNT>)((ObjectQuery)query).Execute(MergeOption.AppendOnly);

            if (result != null)
            {
                List<VENDOR_ACCOUNT> list = result.ToList();
                comboBoxName.ItemsSource = list;
                if (list != null && list.Count != 0)
                {
                    comboBoxName.SelectedItem = list.First();
                }
                else
                {
                    comboBoxName.ItemsSource = null;
                }
            }
            else
            {
                comboBoxName.ItemsSource = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject AddNewEntityObject()
        {
            VENDOR_ACCOUNT newUser = null;
            var q = from VENDOR_ACCOUNT in Context.VENDOR_ACCOUNTS where VENDOR_ACCOUNT.FirstName.Equals("FirstName") && VENDOR_ACCOUNT.LastName.Equals("LastName") select VENDOR_ACCOUNT;

            if (q.Count() == 0)
            {
                newUser = new VENDOR_ACCOUNT();

                newUser.FirstName = "FirstName";
                newUser.LastName = "LastName";
                newUser.CompanyName = "CompanyName";
                newUser.Active = true;
                newUser.AccountLimit = 1000M;
                newUser.AccountTerms = "Amount Due Upon Receipt";
                newUser.CreateDate = DateTime.Now;
                Context.VENDOR_ACCOUNTS.AddObject(newUser);
            }
            else
            {
                newUser = q.First();

                if (newUser.IsDeleted)
                {
                    newUser.CreateDate = DateTime.Now;
                    newUser.IsDeleted = false;
                }
            }

            return newUser;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return comboBoxName;
        }

        /// <summary>
        /// Assure that the user has entered all of the required data.
        /// </summary>
        public override bool VerifyFormEntries()
        {
            ///Make sure the bare essentials are filled in.
            if (tbfirstNameTextBox.Text.Count() != 0 && tblastNameTextBox.Text.Count() != 0 && tbCompanyName.Text.Count() != 0 && (tbHighLimit.Text.Count() != 0 || radioNoLimit.IsChecked.Value))
            {
                return true;
            }
            else
            {
                UMD.ShowGuiMessageSTASafe_NOTHROW("You must enter data in all of the fields marked with an asterisk.");
                return false;
            }
        }
    }
}
